//
//  HttpClient.m
//  tingCheChang
//
//  Created by hff on 15/8/3.
//  Copyright (c) 2015年 threeTi. All rights reserved.
//

#import "PersonHttpClient.h"
#import "NSMutableDictionary+Utils.h"
#import "NSString+Category.h"


#define REQUEST_ADDRESS  @""



@implementation PersonHttpClient
static PersonHttpClient *instance = nil;

#define REQUEST_SIGN  [self createRequestSign:params]

//单例
+(PersonHttpClient *)shareInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[PersonHttpClient alloc] init];
    });
    
    return instance;
}


- (void)checkRegNumber:(NSString *)registrationNumber
                rec_id:(NSString *)rec_id
               success:(TTIClientManagerBlock)success
               failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:registrationNumber forKey:@"registrationNumber"];
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"checkRegNumber/1"];
    
//    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//
//        [self parseResponse:responseObject operation:operation success:success failure:failure];
//
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//
//        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
//
//    }];
    [self.manager POST:URLstring parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self parseResponse:responseObject operation:task success:success failure:failure];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self parseResponse:task operation:task success:success failure:failure];
    }];
    
}


-(NSString *)createRequestSign:(NSMutableDictionary *)paramArray
{

    NSArray *sortedKeys= [paramArray.allKeys sortedArrayUsingSelector:@selector(compare:)];
    
    NSMutableArray *sortedValues = [[NSMutableArray alloc] initWithCapacity:0] ;
    
    for(id key in sortedKeys) {
        id object = [paramArray objectForKey:key];
        [sortedValues addObject:object];
    }
    
    __block NSString *sign = @"";
    [sortedKeys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
      
        sign = [sign stringByAppendingString:[NSString stringWithFormat:@"%@=%@&",(NSString *)obj,[sortedValues objectAtIndex:idx]]];
    }];
    
     sign = [sign stringByAppendingString:[NSString stringWithFormat:@"key=%@",@"f0ae367316904c1ab0c81509cb598ade"]];
    
    sign = [sign md5];
    
    return sign;
}

#pragma mark - 登陆注册

//登录
- (void)requestUserLoginWithCellNumber:(NSString *)cellNumber
                              password:(NSString *)password
                               success:(TTIClientManagerBlock)success
                               failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:cellNumber    forKey:@"cellNumber"];
    [params addUnEmptyString:password      forKey:@"password"];
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/member/loginApp"];
    
    [self.manager POST:URLstring parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self parseResponse:responseObject operation:task success:success failure:failure];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self parseResponse:task operation:task success:success failure:failure];
    }];
}


//注册
- (void)requestUserRegistWithCellNumber:(NSString *)cellNumber
                                   code:(NSString *)code
                               password:(NSString *)password
                                success:(TTIClientManagerBlock)success
                                failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:cellNumber    forKey:@"cellNumber"];
    [params addUnEmptyString:code      forKey:@"code"];
    [params addUnEmptyString:password      forKey:@"password"];
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/member/regist"];
    
//    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//
//        [self parseResponse:responseObject operation:operation success:success failure:failure];
//
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
//    }];
    [self.manager POST:URLstring parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self parseResponse:responseObject operation:task success:success failure:failure];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self parseResponse:task operation:task success:success failure:failure];
    }];
}


//验证码
//- (void)requestVerifyPhoneCodeWithCellNumber:(NSString *)cellNumber
//                                     success:(TTIClientManagerBlock)success
//                                     failuer:(TTIClientManagerBlock)failure
//{
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params addUnEmptyString:cellNumber    forKey:@"cellNumber"];
//    
//    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/member/sendVerifyCode"];
//    
//    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        
//        [self parseResponse:responseObject operation:operation success:success failure:failure];
//        
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
//    }];
//}


//完善个人信息
- (void)requestFinishUserInfoWithCellNumber:(NSString *)cellNumber
                                  headPhoto:(UIImage *)headPhoto
                                   nickName:(NSString *)nickName
                                     gender:(NSString *)gender
                                    country:(NSString *)country
                                    success:(TTIClientManagerBlock)success
                                    failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:cellNumber    forKey:@"cellNumber"];
    [params addUnEmptyString:nickName    forKey:@"nickName"];
    [params addUnEmptyString:gender    forKey:@"gender"];
    [params addUnEmptyString:country    forKey:@"country"];
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/member/completeInfo"];
    
//    [self.manager POST:URLstring parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formDta) {
//
//        NSData *imageData = UIImageJPEGRepresentation(headPhoto, 0.8);
//        if (imageData)
//        {
//            [formDta appendPartWithFileData:imageData name:@"headPhoto" fileName:@"headPhoto.jpg" mimeType:@"image/jpeg"];
//        }
//
//    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
//
//        [self parseResponse:responseObject operation:operation success:success failure:failure];
//
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//
//        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
//
//    }];
}


//验证验证码
//- (void)requestVerifyCodeWithCellNumber:(NSString *)cellNumber
//                                   verityCode:(NSString *)verityCode
//                                success:(TTIClientManagerBlock)success
//                                failuer:(TTIClientManagerBlock)failure
//{
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params addUnEmptyString:cellNumber    forKey:@"cellNumber"];
//    [params addUnEmptyString:verityCode    forKey:@"verityCode"];
//    
//
//    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/member/checkVerifyCode"];
//    
//    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        
//        [self parseResponse:responseObject operation:operation success:success failure:failure];
//        
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
//    }];
//}


//重置密码
- (void)requestResetPasswordWithCellNumber:(NSString *)cellNumber
                                   code:(NSString *)code
                                 newPwd:(NSString *)newPwd
                                success:(TTIClientManagerBlock)success
                                failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:cellNumber    forKey:@"cellNumber"];
    [params addUnEmptyString:code    forKey:@"code"];
    [params addUnEmptyString:newPwd    forKey:@"newPwd"];
    
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/member/resetNewPwdByCode"];
    
//    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//
//        [self parseResponse:responseObject operation:operation success:success failure:failure];
//
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
//    }];
    
    [self.manager POST:URLstring parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self parseResponse:responseObject operation:task success:success failure:failure];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self parseResponse:task operation:task success:success failure:failure];
    }];
}


#pragma mark - 国家列表
- (void)requestCountryListWithNextPage:(NSString *)nextPage
                              pageSize:(NSString *)pageSize
                               success:(TTIClientManagerBlock)success
                               failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:nextPage    forKey:@"nextPage"];
    [params addUnEmptyString:pageSize    forKey:@"pageSize"];
   
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/country/findCountryList"];
    
//    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//
//        [self parseResponse:responseObject operation:operation success:success failure:failure];
//
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
//    }];
    [self.manager POST:URLstring parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self parseResponse:responseObject operation:task success:success failure:failure];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self parseResponse:task operation:task success:success failure:failure];
    }];
}


#pragma mark - 意见反馈
//意见反馈
- (void)requestAddFeedbackWithMemberId:(NSString *)memberId
                              feedtype:(NSString *)feedtype
                               content:(NSString *)content
                           contactInfo:(NSString *)contactInfo
                               success:(TTIClientManagerBlock)success
                               failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:memberId    forKey:@"memberId"];
    [params addUnEmptyString:feedtype    forKey:@"feedtype"];
    [params addUnEmptyString:content    forKey:@"content"];
    [params addUnEmptyString:contactInfo    forKey:@"contactInfo"];
    
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/feedback/sendSuggestion"];
    
//    [self.manager POST:URLstring parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//
//        [self parseResponse:responseObject operation:operation success:success failure:failure];
//
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        [self parseResponse:operation.responseObject operation:operation success:success failure:failure];
//    }];
    
    [self.manager POST:URLstring parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self parseResponse:responseObject operation:task success:success failure:failure];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self parseResponse:task operation:task success:success failure:failure];
    }];
}


//版本更新
- (void)requestUpdateVersionWithUserDevice:(NSString *)userDevice
                                appVersion:(NSString *)appVersion
                                   success:(TTIClientManagerBlock)success
                                   failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:userDevice    forKey:@"userDevice"];
    [params addUnEmptyString:appVersion    forKey:@"appVersion"];
   
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/appVersion/findUpdateInfo"];

    
    [self.manager POST:URLstring parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self parseResponse:responseObject operation:task success:success failure:failure];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self parseResponse:task operation:task success:success failure:failure];
    }];
}


//消息列表
- (void)requestMessageListWithNextPage:(NSString *)nextPage
                              pageSize:(NSString *)pageSize
                               success:(TTIClientManagerBlock)success
                               failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:nextPage    forKey:@"nextPage"];
    [params addUnEmptyString:pageSize    forKey:@"pageSize"];
    
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/message/findMessageList"];
    
    [self.manager POST:URLstring parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self parseResponse:responseObject operation:task success:success failure:failure];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self parseResponse:task operation:task success:success failure:failure];
    }];

}


//消息详情
- (void)requestMessageDetialWithModelId:(NSString *)modelId
                                success:(TTIClientManagerBlock)success
                                failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:modelId    forKey:@"modelId"];
    
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/message/findMessageById"];
    
    [self.manager POST:URLstring parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self parseResponse:responseObject operation:task success:success failure:failure];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self parseResponse:task operation:task success:success failure:failure];
    }];

}
//公告信息
- (void)requestNoticeListWithNextPage:(NSString *)nextPage
                             pageSize:(NSString *)pageSize
                               status:(NSString *)status
                             sortData:(NSString *)sortData
                               userId:(NSString *)userId
                              success:(TTIClientManagerBlock)success
                              failuer:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:nextPage    forKey:@"nextPage"];
    [params addUnEmptyString:pageSize    forKey:@"pageSize"];
    [params addUnEmptyString:status      forKey:@"status"];
//    [params addUnEmptyString:sortData    forKey:@"sortData"];
    [params addUnEmptyString:userId      forKey:@"userId"];
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/announcement/findAnnouncementList"];
    
    [self.manager POST:URLstring parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self parseResponse:responseObject operation:task success:success failure:failure];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self parseResponse:task operation:task success:success failure:failure];
    }];
}
//公告详情
- (void)requestNoticeDetailWithId:(NSString *)modelId
                           userId:(NSString *)userId
                          success:(TTIClientManagerBlock)success
                          failure:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:modelId    forKey:@"modelId"];
    [params addUnEmptyString:userId    forKey:@"userId"];
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/announcement/findAnnouncementById"];
    
    [self.manager POST:URLstring parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self parseResponse:responseObject operation:task success:success failure:failure];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self parseResponse:task operation:task success:success failure:failure];
    }];
    
}
#pragma mark - 查询手机号是否已注册
- (void)requestCheckCellNumberRegistedWithcellNumber:(NSString *)cellNumber
                                  success:(TTIClientManagerBlock)success
                                  failure:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:cellNumber    forKey:@"cellNumber"];
    
     NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/member/phoneRegistCheck"];
    [self.manager POST:URLstring parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self parseResponse:responseObject operation:task success:success failure:failure];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self parseResponse:task operation:task success:success failure:failure];
    }];
}

#pragma mark - 添加飞行记录
- (void)requestSaveFlyRecordWithName:(NSString *)name
                             flyDate:(NSString *)flyDate
                              userId:(NSString *)userId
                             success:(TTIClientManagerBlock)success
                             failure:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:name    forKey:@"name"];
    [params addUnEmptyString:flyDate    forKey:@"flyDate"];
    [params addUnEmptyString:userId    forKey:@"userId"];
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/firmware/saveFlyRecord"];
    [self.manager POST:URLstring parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self parseResponse:responseObject operation:task success:success failure:failure];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self parseResponse:task operation:task success:success failure:failure];
    }];

}

#pragma mark - 查询飞行记录
- (void)requestFindFlyRecordListNextPage:(NSString *)nextPage
                                pageSize:(NSString *)pageSize
                              jsonFilter:(NSString *)jsonFilter
                                 success:(TTIClientManagerBlock)success
                                 failure:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:nextPage    forKey:@"nextPage"];
    [params addUnEmptyString:pageSize    forKey:@"pageSize"];
    
    //jsonFilter控制器传过来 userId,用userId拼接jsonFilter对象
    NSString *jsonKey = @"search_EQ_createUser";
    NSString *json = [NSString stringWithFormat:@"{'%@':'%@'}",jsonKey,jsonFilter];
    [params addUnEmptyString:json forKey:@"jsonFilter"];

    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/firmware/findFlyRecordList"];
    [self.manager POST:URLstring parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self parseResponse:responseObject operation:task success:success failure:failure];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self parseResponse:task operation:task success:success failure:failure];
    }];

}



#pragma mark - 我的消息小红点
- (void)requestCheckNewMsgWithuserId:(NSString *)userId
                             success:(TTIClientManagerBlock)success
                             failure:(TTIClientManagerBlock)failure
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params addUnEmptyString:userId    forKey:@"userId"];
    
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/announcement/findNewAnnouncement"];
    [self.manager POST:URLstring parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self parseResponse:responseObject operation:task success:success failure:failure];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self parseResponse:task operation:task success:success failure:failure];
    }];

}

#pragma mark - app下载
- (void)requestDownloadNewAppsuccess:(TTIClientManagerBlock)success
                             failure:(TTIClientManagerBlock)failure
{
    NSString *URLstring = [NSString stringWithFormat:@"%@",@"m/appVersion/appDownload"];
    [self.manager POST:URLstring parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self parseResponse:responseObject operation:task success:success failure:failure];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self parseResponse:task operation:task success:success failure:failure];
    }];

}
@end
